﻿/*
*  ChgDoc CMS - GNU General Public License, version 3 (GPL-3.0)
*  Copyright (C)  2012. Ian Quigley
*  
*  This program is free software: you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation, either version 3 of the License, or
*  (at your option) any later version.
*  
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*/


$(document).ready(function () {

    // Behavior for dialog
    $("#dialog-form").dialog({
        autoOpen: false,
        height: 500,
        width: 550,
        modal: true,
        buttons: {
            Cancel: function () {
                $(this).dialog("close");
            }
        },
        close: function () {
            //allFields.val("").removeClass("ui-state-error");
        }
    });

    // Action for "edit details" button
    $("#editDetails")
			.click(function () {
			    $("#dialog-form").dialog("open");
			    return false;
			}); 

});


var article = {
    id: 0,

    listAll: function () {
        article.listLocations();
        article.listTags();
        article.listUrls();
        article.listAttributeCodes();
    },

    listLocations: function () {
        setTimeout('article.listLocationsAction()', 500);
    },

    listLocationsAction: function () {
        doAjax(appGlobal.serverUrl + "service/location.asmx/List", "{}", function (msg) {
            $('#pageContent_drpLocation').empty();
            for (i = 0; i < msg.d._list.length; i++)
                $('#pageContent_drpLocation').append('<option value="' + msg.d._list[i].Key + '">' + msg.d._list[i].Value + '</option>');
        });
        doAjax(appGlobal.serverUrl + "service/article.asmx/Locations", "{ articleId : " + this.id + "}", function (msg) {
            $('#articleLocation').empty();
            for (i = 0; i < msg.d._list.length; i++) {
                $('#articleLocation').append('<li data-bind="' + msg.d._list[i].Key + '"><span>' + msg.d._list[i].Value + '</span><a class="x" onclick="article.deleteLocation(' + msg.d._list[i].Key + ')">X</a> </li>');
                $('#pageContent_drpLocation option[value=' + msg.d._list[i].Key + ']').remove();
            }

            if ($('#pageContent_drpLocation option').size() == 0)
                $('div.addLocationCtrl').hide(200);
            else
                $('div.addLocationCtrl').show(200);
        });
    },
    deleteLocation: function (locationId) {
        doAjax(appGlobal.serverUrl + "service/article.asmx/DeleteLocation", "{ articleId : " + this.id + ", locationId : " + locationId + "}", this.listLocations());
    },

    addLocation: function () {
        var locationId = $('#pageContent_drpLocation option:selected').val();
        doAjax(appGlobal.serverUrl + "service/article.asmx/AddLocation", "{ articleId : " + this.id + ", locationId : " + locationId + "}", this.listLocations());
        return false;
    },

    listTags: function () {
        setTimeout('article.listTagsAction()', 500);
    },

    listTagsAction: function () {
        doAjax(appGlobal.serverUrl + "service/Tag.asmx/List", "{}", function (msg) {
            $('#pageContent_drpTag').empty();
            for (i = 0; i < msg.d._list.length; i++)
                $('#pageContent_drpTag').append('<option value="' + msg.d._list[i].Key + '">' + msg.d._list[i].Value + '</option');
        });
        doAjax(appGlobal.serverUrl + "service/article.asmx/Tags", "{ articleId : " + this.id + "}", function (msg) {
            $('#articleTag').empty();
            for (i = 0; i < msg.d._list.length; i++) {
                $('#articleTag').append('<li data-bind="' + msg.d._list[i].Key + '"><span>' + msg.d._list[i].Value + '</span><a class="x" onclick="article.deleteTag(' + msg.d._list[i].Key + ')">X</a> </li>');
                $('#pageContent_drpTag option[value=' + msg.d._list[i].Key + ']').remove();
            }

            if ($('#pageContent_drpTag option').size() == 0)
                $('div.addTagCtrl').hide(200);
            else
                $('div.addTagCtrl').show(200);
        });
    },
    deleteTag: function (tagId) {
        doAjax(appGlobal.serverUrl + "service/article.asmx/DeleteTag", "{ articleId : " + this.id + ", tagId : " + tagId + "}", this.listTags());
    },

    addTag: function () {
        var tagId = $('#pageContent_drpTag option:selected').val();
        doAjax(appGlobal.serverUrl + "service/article.asmx/AddTag", "{ articleId : " + this.id + ", tagId : " + tagId + "}", this.listTags());
        return false;
    },


    listUrls: function () {
        setTimeout('article.listUrlAction()', 500);
    },
    listUrlAction: function () {

        doAjax(appGlobal.serverUrl + "service/article.asmx/Urls", "{ articleId : " + this.id + "}", function (msg) {
            $('#articleUrlList').empty();
            for (i = 0; i < msg.d._list.length; i++)
                $('#articleUrlList').append('<li data-bind="' + msg.d._list[i].Key + '" style="width:90%"><span>' + msg.d._list[i].Value + '</span><a class="x" onclick="article.deleteUrl(' + msg.d._list[i].Key + ')">X</a> </li>');
        });
    },
    addUrl: function () {
        var txt = $('#articleUrl').val();
        var templateId = $('#pageContent_drpTemplate option:selected').val();
        doAjax(appGlobal.serverUrl + "service/article.asmx/AddUrl", "{ articleId : " + this.id + ", templateId  : " + templateId + ", url : '" + txt + "' }", this.listUrls());
        return false;
    },
    deleteUrl: function (urlId) {
        doAjax(appGlobal.serverUrl + "service/article.asmx/DeleteUrl", "{ articleId : " + this.id + ", urlId : " + urlId + "}", this.listUrls());
    },

    listAttributeCodes: function () {

        doAjax(appGlobal.serverUrl + "service/article.asmx/Attributes", "{ articleId : " + this.id + "}", function (msg) {
            $('#articleAttribute').empty();
            for (i = 0; i < msg.d._list.length; i++) {
                var val = msg.d._list[i].Value;
                if (val.length > 30) {
                    val = "<a href='#' onclick='article.editAttributeTextLong(\"" + msg.d._list[i].Key + "\", \"" + escape(msg.d._list[i].Value) + "\")'>" + msg.d._list[i].Value.substr(0,30) + "...</a>";
                }
                $('#articleAttribute').append('<li data-bind="' + msg.d._list[i].Key + '" style="width:90%"><span>' + msg.d._list[i].Key + ' = ' + val + '</span><a class="x" onclick="article.deleteAttribute(\'' + msg.d._list[i].Key + '\')">X</a> </li>');
            }
        });
    },

    addAttribute: function (code, val) {

        if (code === undefined)
            code = $('#pageContent_drpListAttributes option:selected').val();

        if (val === undefined)
            val = $('#attributeValue').val();

        doAjax(appGlobal.serverUrl + "service/article.asmx/EditAttribute", "{ articleId : " + this.id + ", code : '" + code + "', value : '" + val + "'}", this.listAttributeCodes());
    },

    editAttributeTextLong: function (code, text) {

        var editText = new editTextDialog({
            title: 'Edit value',
            text: unescape(text),
            ok: function (text) {
                var submitData = '{ code : "' + code + '", : "' + text + '"}';
                article.addAttribute(code, text);
            }
        });

        editText.show();
    },

    deleteAttribute: function (code) {
        doAjax(appGlobal.serverUrl + "service/article.asmx/DeleteAttribute", "{ articleId : " + this.id + ", code :'" + code + "'}", this.listAttributeCodes());
    }
};